<?php

namespace app\controllers;

use Yii;
use app\commands\Service;
use app\models\Comeway;
use yii\filters\VerbFilter;

use app\models\Statistics;
use app\models\User;
use app\models\Workgroup;

/**
 * StatisticsController implements the CRUD actions for Patientpower model.
 */
class StatisticsController extends Service
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function beforeAction($action)
    {
        $this->getView()->params['left_nav_menus'] = [
            ['url' => 'statistics/index', 'name' => '统计首页'],
            //['url' => 'statistics/workgroup', 'name' => '班次统计'],
            ['url' => 'statistics/waiter', 'name' => '咨询数据'],
            ['url' => 'statistics/way', 'name' => '渠道汇总'],
            // ['url' => 'user/worktable', 'name' => '排班表'],
            //['url' => 'statistics/nowmonth', 'name' => '图形分析'],
        ];
        return parent::beforeAction($action);
    }

    /**
     * Lists all Patientpower models.
     * @return mixed
     */
    public function actionIndex()
    {
        //本月
        $now_month_string = date('Y-m');
        $now_month = Statistics::get_month_statistic($now_month_string);
        $now_month['plan'] = 800;
        $now_month['per'] = number_format((int)$now_month['arrive'] * 100 / (int)$now_month['plan'], 2);
        $now_month['estimate'] = number_format(((int)$now_month['arrive'] / (int)date('d')) * (int)date('t'), 0);

        //上月
        $last_month_string = date('Y-m', strtotime('-1 month'));
        $last_month_this_day = date('Y-m-d', strtotime('-1 month'));
        $last_month_all = Statistics::get_month_statistic($last_month_string);
        //上月截止到今天的日期
        $last_month = Statistics::get_between_statistic($last_month_string . '-01', $last_month_this_day);

        //十天内的录单、预约和到诊情况
        $date_end = date('Y-m-d');
        $date_start = date('Y-m-d', strtotime('-10 day'));
        $ten_days = Statistics::get_days_statistic($date_start, $date_end);

        //明天应到诊
        $tomorow_date_string = date('Y-m-d', strtotime('+1 day'));
        $tomorow_should_arrive_list = Statistics::get_days_statistic($tomorow_date_string, $tomorow_date_string);
        $tomorow_should_arrive = $tomorow_should_arrive_list[$tomorow_date_string]['should_arrive'];

        $waiter_last_month = Statistics::get_workgroup_month_data($last_month_string);
        $waiter_now_month = Statistics::get_workgroup_month_data($now_month_string);


        return $this->render('index', [
            'ten_days' => $ten_days,
            'last_month_all' => $last_month_all,
            'last_month' => $last_month,
            'now_month' => $now_month,
            'tomorow_should_arrive' => $tomorow_should_arrive,
            'waiter_last_month' => $waiter_last_month,
            'waiter_now_month' => $waiter_now_month
        ]);
    }

    /**
     * Lists all Patientpower models.
     * @return mixed
     */
    public function actionWorkgroup()
    {
        $data = [];
        //今天
        $today_string = date('Y-m-d');
        $data['今天'] = Statistics::get_jobs_data($today_string, $today_string);
        //昨天
        $yestoday_string = date('Y-m-d', strtotime('- 1 day'));
        $data['昨天'] = Statistics::get_jobs_data($yestoday_string, $today_string);
        //最近7天
        $recent_seven_string = date('Y-m-d', strtotime('- 1 day'));
        $data['最近7天'] = Statistics::get_jobs_data($recent_seven_string, $today_string);
        //本月
        $now_month_string = date('Y-m-01');
        $data['本月'] = Statistics::get_jobs_data($now_month_string, $today_string);
        //上月同期
        $last_monty_start = date('Y-m-01', strtotime('- 1 month'));
        $last_monty_end = date('Y-m-d', strtotime('- 1 month'));
        $data['上月同期'] = Statistics::get_jobs_data($last_monty_start, $last_monty_end);

        //上月
        $timestamp = strtotime($last_monty_start);
        $mdays = date('t', $timestamp);
        $end_time = date('Y-m-' . $mdays, $timestamp);
        $data['上月'] = Statistics::get_jobs_data($last_monty_start, $end_time);

        $workgroup_list = Workgroup::find()->where("name != '行政班'")->orderBy('sort')->all();
        $workgroup_users = User::find()->select('workgroup')->groupBy('workgroup')->all();
        $workgroup_array = [];
        foreach ($workgroup_list as $w) {
            $workgroup_array[$w->name] = 1;
        }
        foreach ($workgroup_users as $u) {
            $workgroup_array[$u->workgroup] = 1;
        }
        $workgroup = array_keys($workgroup_array);
        rsort($workgroup);

        return $this->render('workgroup', ['data' => $data, 'workgroup' => $workgroup]);
    }

    public function actionWaiter()
    {
        $month = Yii::$app->request->get('month', date('Y-m'));
        $type = Yii::$app->request->get('type', 'arrive');
        if ('undefined' == $type) {
            $type = 'arrive';
        }

        if ($type == 'arrive') {
            $type_field = 'arrive_time';
        }
        if ($type == 'order') {
            $type_field = 'order_time';
        }
        if ($type == 'create') {
            $type_field = 'createtime';
        }
        $sql_create = "select user_id, date({$type_field}) as pdate, count(id) as numc from yyh_patient where {$type_field} like '{$month}%' group by user_id, pdate order by user_id";
        $rs_create = \app\models\Db::sql($sql_create);
        $data = [];
        if ($rs_create != null && count($rs_create) > 0) {
            foreach ($rs_create as $v) {
                $data[$v['user_id']][$v['pdate']] = $v['numc'];
            }
        }
        $users = [];
        $user_list = User::find()->all();
        if ($user_list != null && count($user_list) > 0) {
            foreach ($user_list as $v) {
                $users[$v->id] = $v->showname;
            }
        }

        return $this->render(
            'waiter',
            ['month' => $month, 'type' => $type, 'data' => $data, 'users' => $users]
        );
    }

    public function actionWay()
    {
        //时间段
        $month_start = Yii::$app->request->get('month_start', date('Y-m-01'));
        $month_end = Yii::$app->request->get('month_end', date('Y-m-d'));

        $sql_create = "select comeway_id, count(id) as numc from yyh_patient where arrive_time between '{$month_start}' and '{$month_end}' group by comeway_id order by comeway_id";
        $rs_create = \app\models\Db::sql($sql_create);

        $data = [];
        if (null != $rs_create && count($rs_create) > 0) {
            foreach ($rs_create as $d) {
                $way_name = Comeway::findOne($d['comeway_id'])->name;
                $data[$way_name] = $d['numc'];
            }
        }

        return $this->render('way', ['data' => $data, 'month_start' => $month_start, 'month_end' => $month_end]);
    }

    public function actionGraph()
    {
        //本月
        $now_month_string = date('Y-m');



        return $this->render('index', []);
    }
}
